//
//  MLTBusinessesListViewController.m
//  MotherLover
//
//  Created by lanou on 16/1/12.
//  Copyright © 2016年 zj. All rights reserved.
//



#import "MLTBusinessesListViewController.h"

#import "MLTBusinessListTableViewCell.h"

#import "zJRequestManager.h"

#import "MLTBusinessesModel.h"

@interface MLTBusinessesListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *BusinessArray;

@end

@implementation MLTBusinessesListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    
    [self setUpData];
    
    
    
    
}

//http://api.dianping.com/v1/business/find_businesses?appkey=32976344&city=%E5%8C%97%E4%BA%AC&page=1&sort=1&limit=20&keyword=%E6%97%A9%E6%95%99%E4%B8%AD%E5%BF%83&platform=2&sign=C9C1AC12BBB61366FB92D1B532DC3E7CE49FEF23

//上海, 婴儿游泳:http://api.dianping.com/v1/business/find_businesses?appkey=32976344&city=%E4%B8%8A%E6%B5%B7&page=1&sort=1&limit=20&keyword=%E5%A9%B4%E5%84%BF%E6%B8%B8%E6%B3%B3&platform=2&sign=AE0AA8DFEE440B4F28CE8B018933EDF3D1C663C4


- (void)setUpData
{
    NSLog(@"%@", _area);
    NSLog(@"%@", _city);
    NSLog(@"%@", _keyWord);
    NSString *cityUrl = [_city stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *keyUrl = [_keyWord stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSString *url = [NSString stringWithFormat:@"http://api.dianping.com/v1/business/find_businesses?appkey=32976344&city=%@&page=1&sort=1&limit=20&keyword=%@&platform=2&sign=C9C1AC12BBB61366FB92D1B532DC3E7CE49FEF23", cityUrl, keyUrl];
    NSLog(@"url %@", url);
    
    NSString *kkUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [zJRequestManager requestWithUrl:kkUrl parmDic:nil method:GET succeedWithData:^(NSData *data) {
        
        NSLog(@"%@", data);
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSArray *businessesArray = dic[@"businesses"];
        
        _BusinessArray = [NSMutableArray array];
        
        for (NSDictionary *businessesDic in businessesArray) {
            MLTBusinessesModel *model = [[MLTBusinessesModel alloc] init];
            [model setValuesForKeysWithDictionary:businessesDic];
            [_BusinessArray addObject:model];
        }
        
        NSLog(@"%@", _BusinessArray);
        
        [self.tableView reloadData];
        
    } failedWithError:^(NSError *connectionError) {
        
        NSLog(@"connectionError %@", connectionError);
        
    }];
    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _BusinessArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"BusinessListCell";
    MLTBusinessListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MLTBusinessListTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    
    
    UIButton *addressButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    addressButton.frame = CGRectMake(KW, 30, 100, 50);
    addressButton.backgroundColor = [UIColor lightGrayColor];
    addressButton.tag = indexPath.row;
    addressButton.alpha = 0.5;
    [cell.contentView addSubview:addressButton];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    
    addressButton.frame = CGRectMake(KW - 100, 30, 100, 50);
    
    [UIView commitAnimations];
    
    
    //传值 model 赋值 cell
    MLTBusinessesModel *model = _BusinessArray[indexPath.row];
    
    cell.model = model;
  
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return KW / 4 - 10;
}

//- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UIButton *button = [cell.contentView viewWithTag:indexPath.row];
////    [button removeFromSuperview];
//    
//    button.hidden = YES;
//}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
